package org.darkforest.ucenter.service.impl;

import org.darkforest.model.Member;
import org.darkforest.model.MemberWithBLOBs;
import org.darkforest.ucenter.service.MemberPathService;
import org.darkforest.ucenter.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * MemberPathServiceImpl
 * Created by Justin on 15/5/24.
 */
@Service
public class MemberPathServiceImpl implements MemberPathService {
    @Autowired
    @Qualifier("memberServiceImpl")
    private MemberService memberService;

    @Override
    public void setPath(Integer memberId, String path) {
        MemberWithBLOBs member = new MemberWithBLOBs();
        member.setMemberId(memberId);
        member.setPath(path);

        memberService.updateMember(member);
    }

    @Override
    public String getPath(Integer memberId) {
        Member member = memberService.findMemberById(memberId);
        return member.getPath();
    }

    @Override
    public void appendTo(Integer memberId, Integer appendToMember) {
        String path = getPath(appendToMember);
        setPath(memberId, path + "," + memberId);
    }

    @Override
    public void setToRoot(Integer memberId) {
        setPath(memberId, "" + memberId);
    }

}
